import 'dart:convert';
import 'package:http/http.dart';

class DartAPI {
  // ignore: constant_identifier_names
  static const String HOST = "192.168.7.35:9981"; // localhost
  static const String API_STATION = "/station";

  //常用的POST请求
  static _postREQ(String path, data) async {
    var requestBody = jsonEncode(data);
    var url = Uri.http(HOST, path);
    var response = await post(url,
        headers: {"Content-Type": "application/json"}, body: requestBody);

    if (response.statusCode == 200) {
      var res = jsonDecode(response.body);
      return res;
    } else {
      return null;
    }
  }

//常用的GET请求
  static _getREQ(String path) async {
    var url = Uri.http(HOST, path);
    var response = await get(url);
    if (response.statusCode == 200) {
      var res = jsonDecode(response.body);
      return res;
    } else {
      return null;
    }
  }

  //获取天气信息
  static findWeather(lat, lng) async {
    var data = {
      "lat": lat,
      "lng": lng,
    };
    return await _postREQ('$API_STATION/weather', data);
  }

  //获取公交车站信息
  static findAllBusStation(lat, lng) async {
    var data = {
      "lat": lat,
      "lng": lng,
    };
    return await _postREQ('$API_STATION/bussite', data);
  }

  //获取共享单车信息
  static findAllByciclePark(lat, lng) async {
    var data = {
      "lat": lat,
      "lng": lng,
    };
    return await _postREQ('$API_STATION/park', data);
  }

  //获取地铁站点信息
  static findAllSubway(lat, lng) async {
    var data = {
      "lat": lat,
      "lng": lng,
    };
    return await _postREQ('$API_STATION/subway', data);
  }

  //获取公交线路信息
  static findAllBusLine(line) async {
    return await _getREQ('$API_STATION/busline/$line');
  }

  //根据线路获取地铁站点信息
  static findAllSubwayByLine(line) async {
    return await _getREQ('$API_STATION/subwayline/$line');
  }

  //根据单车停放点名称获取单车信息
  static findBikeByParkName(name) async {
    return await _getREQ('$API_STATION/bike/$name');
  }
}
